package Demo16_30.Test29;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by 14229 on 2020/1/31.
 */
public class Server {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(12345);
        System.out.println("等待连接中");
        Socket accept = serverSocket.accept();


        InetAddress inetAddress = accept.getInetAddress(); //得到ip地址对象
        String hostAddress = inetAddress.getHostAddress(); //ip地址字符串

        System.out.println("链接成功:"+hostAddress);


        //获取服务器的输入流
        InputStream is = accept.getInputStream();
        //创建 目的地字节输出流

        BufferedOutputStream bf = new BufferedOutputStream(new FileOutputStream("29TCP.jpg"));

        byte[] bytes = new byte[1024];
        int len = -1 ;

        while ((len = is.read(bytes))!=-1){
            bf.write(bytes,0,len);
        }


        //响应信息
        OutputStream os = accept.getOutputStream();

        os.write("图片上传成功".getBytes());

        //关闭资源
        os.close();
        bf.close();
        is.close();

        accept.close();
        serverSocket.close();

    }
}
